package com.corechina.apex_osswords.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;


@Data
@ApiModel("数据返回")
public class Response implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final String SUCCESS = "success";

    @ApiModelProperty("code")
    private int code;

    @ApiModelProperty("信息")
    private Object info;

    @ApiModelProperty("返回数据")
    private Object data;

    @ApiModelProperty("返回数据计数")
    private Integer count;

    @ApiModelProperty("是否错误")
    private boolean error;

    public Response(int code, Object info, Object data, Integer count, boolean error) {
        this.code = code;
        this.info = info;
        this.data = data;
        this.count =count;
        this.error = error;
    }

    public static Response error() {
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "未知异常，请联系管理员");
    }

    public static Response error(String msg) {
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
    }

    public static Response error(int code, Object msg) {
        return new Response(code, msg, null,1, true);
    }

    public static Response ok() {
        return ok(SUCCESS, null,1);
    }

    public static Response ok(Object data) {
        return ok(SUCCESS, data,1);
    }

    public static Response ok(Object data,Object count) {
        return ok(SUCCESS, data,count);
    }

    public static Response ok(String msg, Object data, Object count) {
        return new Response(HttpStatus.OK.value(), msg, data, Integer.valueOf(String.valueOf(count)),false);
    }


}
